package zhuzx.generator;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import javax.swing.JOptionPane;

import org.zhuzx.util.GeneralUtil;
import org.zhuzx.util.MicroUtil;
import org.zhuzx.util.UnifiedConstants;

public class GuiUtil {
	
	public static void sendToClipboard(String str) {
		StringSelection ss = new StringSelection(str);
		getClipboard().setContents(ss, null);
	}
	
	public static String getTextFromClipboard() {
		Transferable clipTf = getClipboard().getContents(null);
		if (clipTf != null && clipTf.isDataFlavorSupported(DataFlavor.stringFlavor)) {
			try {
				return (String) clipTf.getTransferData(DataFlavor.stringFlavor);
			} catch (UnsupportedFlavorException | IOException e) {
				throw new RuntimeException(e);
			}
		}
		return null;
	}
	
	private static Clipboard getClipboard() {
		return Toolkit.getDefaultToolkit().getSystemClipboard();
	}
	
	public static boolean confirm(String message) {
		int result = JOptionPane.showConfirmDialog(null, message, null, JOptionPane.YES_NO_OPTION);
		return result == JOptionPane.YES_OPTION;
	}
	
	@SuppressWarnings("unchecked")
	private static <T> T getContainerForShutdownHook(String key, Supplier<T> supplier) {
		T container = (T) UnifiedConstants.getRuntimeConstant(key);
		if (container == null) {
			container = supplier.get();
			UnifiedConstants.addRuntimeConstant(key, container);
			Runtime.getRuntime().addShutdownHook(new Thread(() ->
					System.out.println("ShutdownHook-" + key + "：\n" +
					UnifiedConstants.getRuntimeConstant(key))));
		}
		return container;
	}
	
	/**
	 * 将文本消息暂存至运行结束时统一输出
	 */
	public static void printOnExit(String msg) {
		String key = "text_print_on_exit";
		StringBuilder text = getContainerForShutdownHook(key, StringBuilder::new);
		text.append(msg).append("\n");
	}
	
	public static void countOnExit(String key, int value) {
		String innerKey = "integer_count_on_exit" + '-' + key;
		List<Integer> total = getContainerForShutdownHook(innerKey, () -> Collections.singletonList(0));
		total.set(0, total.get(0) + value);
	}

	/**
	 * 将数据以醒目方式打印到控制台，带标题和边框。
	 * @param list 需要输出的数据内容
	 * @param desc 显示在数据上方的标题
	 * @param needEmphasize 是否需要加强醒目
	 */
	public static <K,V> void printf(List<Map<K,V>> list, String desc, boolean needEmphasize) {
		char sep = needEmphasize ? '=' : '-';
		String emphasizeLabel = needEmphasize ? "\n" : "";
		String line = MicroUtil.createRepeatedCharSequence(sep, 16);
		System.out.println(emphasizeLabel + "start" + line + desc + line);
		if (list.size() > 0) {
			System.out.println("\t" + GeneralUtil.getCombinedString(list.get(0).keySet(), "\t"));
		} else {
			System.out.println("无数据");
		}
		for (int i=0; i<list.size(); i++) {
			String values = GeneralUtil.getCombinedString(list.get(i).values(), "\t");
			System.out.println("行" + (i+1) + "\t" + values);
		}
		System.out.println("end" + sep + sep + line + desc + line + emphasizeLabel);
	}
	
}